<?php
namespace app\common\customer\logic;

use app\common\BaseLogic;
use app\common\customer\model\CustomerTag;

class CustomerTagLogic extends BaseLogic
{
    public function getMemberTag($memberId) {
        $cacheName = "member:tag:".$memberId;
        $tags = $this->redis->get($cacheName);

        if (empty($tags)) {
            $memberTagModel = new CustomerTag();
            $rows = $memberTagModel->where('member_id', $memberId)->field('id,category_id, status')->select();

            $tags = [];
            foreach ($rows as $row) {
                $row->append(['category_title', 'status_title']);
                $data = [];
                $data['id'] = $row['id'];
                $data['category'] = $row['category_title'];
                $data['status'] = $row['status_title'];
                array_push($tags, $data);
            }
            $this->redis->set($cacheName, json_encode($tags));
        } else {
            $tags = json_decode($tags, true);
        }

        return $tags;
    }
}